package www.openteam.com.ui.business;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.base.OtherBaseFragment;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.CustomConditon;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.InsertStatus;
import www.openteam.com.bean.QueryCustom;
import www.openteam.com.contract.business.UpdateCustomInfoContract;
import www.openteam.com.presenter.business.UpdateCustomInfoPresenter;
import www.openteam.com.tool.ToastUtil;

/**
 * 更新客户信息
 * Created by kylin on 2016/10/26.
 */

public class UpdateCustomInfoFragment extends OtherBaseFragment<UpdateCustomInfoPresenter>
        implements UpdateCustomInfoContract.UpdateCustomInfoView {

    @Bind(R.id.custom_type)
    public Spinner custom_type;
    @Bind(R.id.moneytype)
    public Spinner moneytype;
    @Bind(R.id.paytype)
    public Spinner paytype;
    QueryCustom.DataBean queryCustom;
    ArrayList<CustomConditon> customConditons;
    ArrayList<CustomType.DataBean> customTypes;
    ArrayList<CustomType.DataBean> moneyTypes;
    ArrayList<CustomType.DataBean> copayTypes;
    int Position;
    public ButtonReturn buttonReturn;
    private MyAdapter customAdapter;
    private MyAdapter moneyAdapter;
    private MyAdapter copayAdapter;
    @Bind(R.id.customno)
    TextView customno;
    @Bind(R.id.customname)
    TextView customname;
    @Bind(R.id.businessman)
    TextView businessman;
    @Bind(R.id.infoamout)
    TextView infoamout;
    @Bind(R.id.address)
    TextView address;
    @Bind(R.id.contact_custom)
    TextView contact_custom;
    @Bind(R.id.phone)
    TextView phone;
    Context mContext;
    final String TAG = UpdateCustomInfoFragment.class.getSimpleName();
    @Bind(R.id.returnButton1)
    Button returnButton1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        queryCustom = getArguments().getParcelable("queryCustom");
        customConditons = getArguments().getParcelableArrayList("customConditons");
        customTypes = getArguments().getParcelableArrayList("customTypes");
        moneyTypes = getArguments().getParcelableArrayList("moneyTypes");
        copayTypes = getArguments().getParcelableArrayList("copayTypes");
        Position = getArguments().getInt("Position");
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_order_entry_detail;
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        returnButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //更新客户信息
                mPresenter.updateCustomInfo(getCode(), getName(), getCType(),
                        getPayTime(), getMType(),
                        getSendAddress(), getConnecter(), getBusinessMan(), getCreditNumb(), getCTel(), queryCustom.getId());
            }
        });
    }

    @Override
    protected void initView() {
        customAdapter = new MyAdapter(customTypes, getActivity());
        moneyAdapter = new MyAdapter(moneyTypes, getActivity());
        copayAdapter = new MyAdapter(copayTypes, getActivity());
        custom_type.setAdapter(customAdapter);
        custom_type.setSelection(customConditons.get(Position).getType());
        moneytype.setAdapter(moneyAdapter);
        moneytype.setSelection(customConditons.get(Position).getMoneytype());
        paytype.setAdapter(copayAdapter);
        paytype.setSelection(customConditons.get(Position).getPaytype());
        customno.setText(queryCustom.getCode());
        customname.setText(queryCustom.getName());
        infoamout.setText(queryCustom.getCreditNumb());
        address.setText(queryCustom.getSendAddress());
        contact_custom.setText(queryCustom.getConnecter());
        businessman.setText(queryCustom.getBusinessMan());
        phone.setText(queryCustom.getCTel());
        returnButton1.setText("更新");
    }

    @Override
    protected void otherViewClick(View view) {

    }


    @Override
    protected UpdateCustomInfoPresenter loadPresenter() {
        return new UpdateCustomInfoPresenter();
    }


    @Override
    public String getCode() {
        return customno.getText().toString();
    }

    @Override
    public String getName() {
        return customname.getText().toString();
    }

    @Override
    public String getCreditNumb() {
        return infoamout.getText().toString();
    }

    @Override
    public String getBusinessMan() {
        return businessman.getText().toString();
    }

    @Override
    public String getSendAddress() {
        return address.getText().toString();
    }

    @Override
    public String getCTel() {
        return phone.getText().toString();
    }

    @Override
    public String getConnecter() {
        return contact_custom.getText().toString();
    }

    @Override
    public String getCType() {
        return returnCode(customTypes, custom_type.getSelectedItemPosition());
    }

    @Override
    public String getPayTime() {
        return returnCode(copayTypes, paytype.getSelectedItemPosition());
    }

    @Override
    public String getMType() {
        return returnCode(moneyTypes, moneytype.getSelectedItemPosition());
    }

    public boolean checkNull() {
        boolean isNull = false;

        if (getCode().isEmpty()) {
            customno.setError("客户编号不能为空");
            isNull = true;
        } else if (getName().isEmpty()) {
            customname.setError("客户名称不能为空");
            isNull = true;
        } else if (getCreditNumb().isEmpty()) {
            infoamout.setError("信息额度不能为空");
            isNull = true;
        } else if (getBusinessMan().isEmpty()) {
            businessman.setError("业务员不能为空");
            isNull = true;
        }
        if (!isNull) {
            createProgressDialog(getString(R.string.updating));
        }
        return isNull;
    }


    @Override
    public void updateCIData(InsertStatus insertStatus) {
        mProgressDialog.dismiss();
        ToastUtil.showToast(getContext(), "更新成功");
        if (buttonReturn != null) {
            Log.d(TAG, "insertCIData: " + customno.getText().toString());
            buttonReturn.clickReturn(customno.getText().toString());
        }

    }


    @Override
    public void hideCIData(Throwable throwable) {
        mProgressDialog.dismiss();
        ToastUtil.showToast(getContext(), throwable.getMessage());
    }


    private String returnCode(ArrayList<CustomType.DataBean> cus, int i) {
        String code = "01";
        code = cus.get(i).getCode();
        return code;
    }

    @Override
    public void onRefresh() {

    }


    public interface ButtonReturn {
        void clickReturn(String code);
    }


    public void buttonSetOnclick(ButtonReturn buttonReturn) {
        this.buttonReturn = buttonReturn;
    }

    class MyAdapter extends BaseAdapter {

        List<CustomType.DataBean> mList = new ArrayList<>();
        Context mContext = null;
        String code = null;

        public MyAdapter(List mList, Context mContext) {
            this.mList = mList;
            this.mContext = mContext;

        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater _LayoutInflater = LayoutInflater.from(mContext);
            convertView = _LayoutInflater.inflate(R.layout.item_user, null);
            if (convertView != null) {
                TextView tv = (TextView) convertView.findViewById(R.id.spinner_item);
                tv.setText(mList.get(position).getName());
            }
            return convertView;
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "onDestroyView: jg");
        customAdapter = null;
        moneyAdapter = null;
        copayAdapter = null;
        custom_type = null;
        moneytype = null;
        paytype = null;
        customTypes = null;
        copayTypes = null;
        moneyTypes = null;
    }

}


